<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/1/20
 * Time: 5:12 PM
 */

namespace api\controllers;

use yii\web\ServerErrorHttpException;
use api\models\UserCertification;

class UserCertificationController extends BaseController
{
    public $modelClass = "api\models\UserCertification";




    public function actionPersonal(){
        $model = UserCertification::findOne(['uid'=>\Yii::$app->getUser()->id,'type'=>UserCertification::PERSONAL_TYPE]);
        return $model;
    }


    public function actionPersonalUpdate(){
        if (\Yii::$app->getRequest()->isPost){
            $model = UserCertification::findOne(['uid'=>\Yii::$app->getUser()->id,'type'=>UserCertification::PERSONAL_TYPE]);
            if (isset($model)){
                if ($model->load(\Yii::$app->getRequest()->getBodyParams(),'')){
                    if ($model->save() === false && ! $model->hasErrors()) {
                        throw new ServerErrorHttpException('Failed to update the object for unknown reason.');
                    }
                    return $model;
                }
            }else{
                $model = new UserCertification();
                if ($model->load(\Yii::$app->getRequest()->getBodyParams(),'')){
                    $model->type = UserCertification::PERSONAL_TYPE;
                    if ($model->save() === false && ! $model->hasErrors()) {
                        throw new ServerErrorHttpException('Failed to update the object for unknown reason.');
                    }
                    return $model;
                }
            }
        }

    }



    public function actionBusiness(){
        $model = UserCertification::findOne(['uid'=>\Yii::$app->getUser()->id,'type'=>UserCertification::BUSINESS_TYPE]);
        $this->checkAccess($this->id, $model);
        return $model;
    }


    public function actionBusinessUpdate(){
        if (\Yii::$app->getRequest()->isPost){
            $model = UserCertification::findOne(['uid'=>\Yii::$app->getUser()->id,'type'=>UserCertification::BUSINESS_TYPE]);
            if (isset($model)){
                if ($model->load(\Yii::$app->getRequest()->getBodyParams(),'')){
                    if ($model->save() === false && ! $model->hasErrors()) {
                        throw new ServerErrorHttpException('Failed to update the object for unknown reason.');
                    }
                    return $model;
                }
            }else{
                $model = new UserCertification();
                $model->type = UserCertification::BUSINESS_TYPE;
                if ($model->load(\Yii::$app->getRequest()->getBodyParams(),'')){
                    if ($model->save() === false && ! $model->hasErrors()) {
                        throw new ServerErrorHttpException('Failed to update the object for unknown reason.');
                    }
                    return $model;
                }
            }
        }
    }



}